package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Fee;
import com.stylefeng.guns.modular.system.service.IFeeService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务科目控制器
 *
 * @author fengshuonan
 * @Date 2018-01-27 18:24:58
 */
@Controller
@RequestMapping("/fee")
public class FeeController extends BaseController {

    private String PREFIX = "/system/fee/";

    @Autowired
    private IFeeService feeService;

    /**
     * 跳转到财务科目首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "fee.html";
    }

    /**
     * 跳转到添加财务科目
     */
    @RequestMapping("/fee_add")
    public String feeAdd() {
        return PREFIX + "fee_add.html";
    }

    /**
     * 跳转到修改财务科目
     */
    @RequestMapping("/fee_update/{feeId}")
    public String feeUpdate(@PathVariable Integer feeId, Model model) {
        Fee fee = feeService.selectById(feeId);
        model.addAttribute("item",fee);
        LogObjectHolder.me().set(fee);
        return PREFIX + "fee_edit.html";
    }

    /**
     * 获取财务科目列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,Integer limit, Integer offset) {
        Page<Map<String, Object>> page=new Page(offset,limit);
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("deptid", ShiroKit.getUser().getDeptId());
        m.put("condition",condition);
        List<Map<String, Object>> fee = feeService.feeList(page,m);
        pageInfoBT.setRows(fee);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 新增财务科目
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Fee fee) {
        fee.setFeeType(1);//自定义类型
        fee.setCreateUserId(ShiroKit.getUser().getId());
        fee.setDeptId(ShiroKit.getUser().getDeptId());
        feeService.insert(fee);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除财务科目
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer feeId) {
       // feeService.deleteById(feeId);
        Fee fee=new Fee();
        fee.setId(feeId);
        fee.setIsDelete(1);
        feeService.updateById(fee);
        return SUCCESS_TIP;
    }

    /**
     * 修改财务科目
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Fee fee) {
        fee.setUpdateTime(new Date());
        feeService.updateById(fee);
        return super.SUCCESS_TIP;
    }

    /**
     * 财务科目详情
     */
    @RequestMapping(value = "/detail/{feeId}")
    @ResponseBody
    public Object detail(@PathVariable("feeId") Integer feeId) {
        return feeService.selectById(feeId);
    }
}
